package com.mall.business.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.business.domain.*;
import com.mall.business.service.*;
import com.mall.common.core.domain.entity.SysUser;
import com.mall.common.utils.DateUtils;
import com.mall.common.utils.SecurityUtils;
import com.mall.common.utils.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mall.business.mapper.TCartMapper;

/**
 * 购物车信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-30
 */
@Service
public class TCartServiceImpl extends ServiceImpl<TCartMapper,TCart>  implements ITCartService
{
    @Autowired
    private TCartMapper tCartMapper;


    @Autowired
    private ITProductService productService;


    @Autowired
    private ITSeckillService tSeckillService;

    @Autowired
    private ITPinkService pinkService;


    @Autowired
    private ITPtActiveService ptActiveService;



    /**
     * 查询购物车信息
     *
     * @param id 购物车信息主键
     * @return 购物车信息
     */
    @Override
    public TCart selectTCartById(String id)
    {
        return tCartMapper.selectTCartById(id);
    }

    /**
     * 查询购物车信息列表
     *
     * @param tCart 购物车信息
     * @return 购物车信息
     */
    @Override
    public List<TCart> selectTCartList(TCart tCart)
    {
        return tCartMapper.selectTCartList(tCart);
    }

    /**
     * 新增购物车信息
     *
     * @param tCart 购物车信息
     * @return 结果
     */
    @Override
    public int insertTCart(TCart tCart)
    {
                tCart.setCreateTime(DateUtils.getNowDate());
            return tCartMapper.insertTCart(tCart);
    }

    /**
     * 修改购物车信息
     *
     * @param tCart 购物车信息
     * @return 结果
     */
    @Override
    public int updateTCart(TCart tCart)
    {
                tCart.setUpdateTime(DateUtils.getNowDate());
        return tCartMapper.updateTCart(tCart);
    }

    /**
     * 批量删除购物车信息
     *
     * @param ids 需要删除的购物车信息主键
     * @return 结果
     */
    @Override
    public int deleteTCartByIds(String[] ids)
    {
        return tCartMapper.deleteTCartByIds(ids);
    }

    /**
     * 删除购物车信息信息
     *
     * @param id 购物车信息主键
     * @return 结果
     */
    @Override
    public int deleteTCartById(String id)
    {
        return tCartMapper.deleteTCartById(id);
    }


    @Override
    public List<TCart> getShopCartItemsByOrderItems(List<String> basketId, OrderItemParam orderItemParam) {
        if (orderItemParam == null && CollectionUtil.isEmpty(basketId)) {
            return  Collections.emptyList();
        }
        // 当立即购买时，没有提交的订单是没有购物车信息的
        if (CollectionUtil.isEmpty(basketId) && orderItemParam != null) {
            // 直接购买
            // 拿到购物车的所有item
            TProduct product = productService.getById(orderItemParam.getProductId());
            TCart shopCartItemDto = new TCart();
            shopCartItemDto.setId(null);
            // 产品Id
            shopCartItemDto.setProductId(orderItemParam.getProductId()+"");
            shopCartItemDto.setProductName(product.getTitle());
            shopCartItemDto.setProductImage(product.getImage());
            // 数量
            shopCartItemDto.setCartNum(orderItemParam.getNum());
            // 设置价格和秒杀 拼团信息
            this.setProductPrice(shopCartItemDto,orderItemParam);
            return Collections.singletonList(shopCartItemDto);
        }
        List<TCart> dbShopCartItems = getShopCartItems();
        // 返回购物车选择的商品信息
        List<TCart> gwcshopItems = dbShopCartItems
                .stream()
                .filter(TGwc -> basketId.contains(TGwc.getId()))
                .collect(Collectors.toList());

        // 设
        gwcshopItems.forEach(tGwc -> tGwc.setType("0"));
        return gwcshopItems;

    }


    public   void  setProductPrice( TCart dto,OrderItemParam orderItemParam ) {
        String productId = dto.getProductId();
        String type = orderItemParam.getType();
        TProduct product = productService.getById(productId);
        BigDecimal ylPrice = product.getYlPrice();
        if("3".equals(type)){
            // 积分
            dto.setProductPrice(product.getYlPrice());
        }else if("4".equals(type)){
            // 积分抽奖
            dto.setProductPrice(BigDecimal.ZERO);
        }else{
            String pinkId = orderItemParam.getPinkId();
            String seckillId = orderItemParam.getSeckillId();
            if(StringUtils.isAllBlank(pinkId,seckillId)){
                // 这个地方价格 应该根据一可一价 和会员等级 设置价格
                BigDecimal vipPrice = productService.getVipPrice(SecurityUtils.getUserIdStr(), productId);
                dto.setProductPrice(vipPrice);
            }else{
                if(StringUtils.isNotBlank(seckillId)){
                    TSeckill seckill = tSeckillService.getById(seckillId);
                    BigDecimal price = seckill.getPrice();
                    dto.setProductPrice(price);
                    dto.setSeckillId(orderItemParam.getSeckillId());
                    // 秒杀

                }else if(StringUtils.isNotBlank(pinkId)){
                    String ptId = orderItemParam.getPtActiveId();
                    TPtActive ptactive = ptActiveService.getById(ptId);
                    BigDecimal price = ptactive.getPrice();
                    dto.setProductPrice(price);
                    dto.setPtId(ptId);
                    dto.setPinkId(pinkId);
                    // 拼团

                }

            }
        }
        dto.setType(type);

    }


    @Override
    public List<TCart> getShopCartItems() {
        TCart  gwc=new TCart();
        gwc.setUserId(SecurityUtils.getUserIdStr());
        List<TCart> tGwcs = this.selectTCartList(gwc);
        return tGwcs;
    }
}
